<?php

namespace App\Http\Controllers\BaseData;

use App\Http\Conf\ResultInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Models\CategoryContract;
use App\Http\Requests\CategoryContractRequest;

class CategoryContractController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $models = CategoryContract::all();
        return view('base_data.category_contract.index', ['models' => $models]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('base_data.category_contract.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CategoryContractRequest $request)
    {
        $model = new CategoryContract();
        $model->name = $request->name;
        $model->is_scale = $request->is_scale;
        if ($model->save()) {
            return redirect()->action('BaseData\CategoryContractController@show', $model);
        } else {
            dd("添加合同分类有异常！");
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = CategoryContract::find($id);
        return view('base_data.category_contract.show', ['model' => $model]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $model = CategoryContract::find($id);
        return view('base_data.category_contract.edit', ['model' => $model]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CategoryContractRequest $request, $id)
    {
        $model = CategoryContract::find($id);
        $model->name = $request->name;
        $model->is_scale = $request->is_scale;
        if ($model->save()) {
            return redirect()->action('BaseData\CategoryContractController@show', $model);
        } else {
            dd("编辑合同分类有异常！");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = CategoryContract::find($id);

        if ($model->delete()) {
            return ['status' => ResultInfo::SUCCESS];
        } else {
            return ['status' => ResultInfo::UNKNOWN];
        }
    }
}
